import cocos
from cocos.director import director
import pyglet
import sys

from cocos.actions import *
class HelloWorld(cocos.layer.Layer):
	is_event_handler = True
	def __init__(self):
		super( HelloWorld, self ).__init__()
		label = cocos.text.Label('Halo Kakak!',
                          font_name='Times New Roman',
                          font_size=32,
                          anchor_x='center', anchor_y='center')
		label.position = 320,240
		self.add( label )

		#sprite
		self.sprite = cocos.sprite.Sprite('grossini.png')
		self.sprite.position = 320,240
		self.sprite.scale = 3
		self.add(self.sprite, z=1 )

		#action
		scale = cocos.actions.ScaleBy(3, duration=2)
		label.do(cocos.actions.Repeat(scale + cocos.actions.Reverse(scale)))
		self.sprite.do(cocos.actions.Repeat(cocos.actions.Reverse(scale) + scale))

		#event
		
		self.text = cocos.text.Label("", x=100, y=280 )
		self.add(self.text)
		self.keys_pressed = set()
		self.update_text()

	def update_text(self):
		key_names = [pyglet.window.key.symbol_string (k) for k in self.keys_pressed]
		text = 'Keys: '+','.join (key_names)
		self.text.element.text = text

	def on_key_press(self, key, modifiers):
		self.keys_pressed.add(key)
		self.update_text()
		if key == pyglet.window.key.ESCAPE:
			sys.exit(0)

	def on_key_release (self, key, modifiers):
		self.keys_pressed.remove (key)
		self.update_text()

	def on_mouse_press (self, x, y, buttons, modifiers):
		self.sprite.position =  director.get_virtual_coordinates (x, y)

cocos.director.director.init(resizable=True)
hello_layer = HelloWorld ()
rotate = cocos.actions.RotateBy(360, duration=10)
#hello_layer.do(cocos.actions.Repeat(rotate + Reverse(rotate)))
main_scene = cocos.scene.Scene (hello_layer)
cocos.director.director.run (main_scene)